﻿using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using MTS.Api;

namespace MTS.Hubs
{
    public class OrderHub : Hub
    {
        private readonly IOrderService _orderService;
        private readonly ILogger _logger;

        public OrderHub(IOrderService orderService,
            ILoggerFactory loggerFactory)
        {
            _orderService = orderService;
            _logger = loggerFactory.CreateLogger<OrderHub>();
        }

        public override async Task OnConnectedAsync()
        {
            await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} joined");
        }

        public override async Task OnDisconnectedAsync(Exception ex)
        {
            await Clients.All.InvokeAsync("Send", $"{Context.ConnectionId} left");
        }

        public Task Send(string message)
        {
            return Clients.All.InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
        }

        public Task Echo(string message)
        {
            return Clients.Client(Context.ConnectionId).InvokeAsync("Send", $"{Context.ConnectionId}: {message}");
        }
    }
}
